package com.atguigu.gulimall.order.controller;

import com.atguigu.gulimall.order.entity.OrderEntity;
import com.atguigu.gulimall.order.entity.OrderReturnReasonEntity;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.UUID;

/**
 * RabbitController
 *
 * @author xiaoping
 * @create 2020/7/3 12:10
 * @since 1.0.0
 */
@Slf4j
@RestController
public class RabbitController {

    @Autowired
    RabbitTemplate rabbitTemplate;

    @GetMapping("/sendMq")
    public  String sendMq(@RequestParam(value = "num",defaultValue = "10") Integer num){
        for (int i = 0; i < 10; i++) {
            if (i%2==0){
                //对象消息，如果发送的消息是个对象，我们会使用序列化机制将对象写出，对象必须实现Serializable接口
                OrderReturnReasonEntity entity = new OrderReturnReasonEntity();
                entity.setId(1L);
                entity.setCreateTime(new Date());
                entity.setName("哈哈-"+i);
                //发送的对象类型的消息，可以是一个json【已经在配置类中配置，可自动转换为json格式】
                rabbitTemplate.convertAndSend("hello-java-exchange", "hello.java",entity,new CorrelationData(UUID.randomUUID().toString()));
            }else {
                OrderEntity entity = new OrderEntity();
                entity.setOrderSn(UUID.randomUUID().toString());
                rabbitTemplate.convertAndSend("hello-java-exchange", "hello.java",entity,new CorrelationData(UUID.randomUUID().toString()));
            }
            log.info("消息发送完成");
        }
        return "ok";
    }


}
